<?php
namespace Driver\Model;
use Think\Model;
class DriverModel extends Model {

    protected $auth_key = 'juanqibanjia';
    /*登录 返回token*/
    public function login( $id,$username)
    {
        $auth_code = authcode( $id."\t".md5( $username.time() ), "ENCODE" , $this->auth_key );
        $this->where(array(
            'id'=>$id,
        ))->save(array(
            'token'=>$auth_code,
            'updatetime'=>time()
        ));
        return $auth_code;
    }
    public function logout()
    {
        session( "AUTH_USER_STRING", null );
        session( "SESSION_KEY", null );

    }
    public function getAuthInfo( $field = NULL )
    {
        $authInfo = authcode( session( "AUTH_USER_STRING"), "DECODE", $this->auth_key );
        $authInfo = explode( "\t", $authInfo );
        $result['user_id'] = $authInfo[0];
        $result['md5'] = $authInfo[1];
        if ( $field )
        {
            if ( !empty($result[$field]) ){
                return $result[$field];
            }else{

                $info = $this->find($result['user_id']);
                return $info[$field];
            }
        }
        return $result;
    }
    public function auth(){
        $token=I('post.token')?I('post.token'):I('get.token');
        $userinfo=$this->where(array('token'=>$token))->find();
        if($userinfo&&$userinfo['updatetime']+14400>time())
            return $userinfo;
        else
            return false;
    }
}
